找传奇、传世资源到传世资源站!

百度人脸识别示例源码(android app)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

package com.example.ai.recognition.activity;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.view.View;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;import com.google.gson.reflect.TypeToken;import com.example.ai.recognition.R;import com.example.ai.recognition.face.baidu.utils.GsonUtils;import com.example.ai.recognition.face.baidu.v3.FaceRecognitionManager;import com.example.ai.recognition.face.baidu.v3.Result;import com.example.ai.recognition.utils.SimpleLog;import org.json.JSONObject;import java.io.File;import java.io.UnsupportedEncodingException;import java.util.HashMap;import io.reactivex.Observable;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.functions.Consumer;import io.reactivex.schedulers.Schedulers;public class RecognitionActivity extends AppCompatActivity { SimpleLog log = new SimpleLog("yy"); static final TypeToken<HashMap<String, Object>> mapToken = new TypeToken<HashMap<String, Object>>() {}; final int TAKE_PHOTO_CODE = 111; final int TAKE_LOCALFILE_CODE = 112; RadioGroup livesRadioGroup, qualityRadioGroup; CheckBox ck_multi; ImageView imageview; TextView tv_message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recognition); livesRadioGroup = findViewById(R.id.livesRadioGroup); qualityRadioGroup = findViewById(R.id.qualityRadioGroup); ck_multi = findViewById(R.id.ck_multi); tv_message = findViewById(R.id.tv_message);// tv_userid = findViewById(R.id.tv_userid);// tv_userinfo = findViewById(R.id.tv_userinfo);// tv_score = findViewById(R.id.tv_score); imageview = findViewById(R.id.imageview); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { log.d("onActivityResult requestCode = " requestCode ",resultCode = " resultCode); switch (requestCode) { case TAKE_PHOTO_CODE: if (resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); imageview.setImageBitmap(bitmap); recognition(bitmap); } else if (resultCode == RESULT_CANCELED) { log.d("用户取消了拍照"); } break; case TAKE_LOCALFILE_CODE: if (resultCode == RESULT_OK) { try { Uri selectedImage = data.getData(); //获取系统返回的照片的Uri String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片 cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String path = cursor.getString(columnIndex); //获取照片路径 cursor.close(); Bitmap bitmap = BitmapFactory.decodeFile(path); imageview.setImageBitmap(bitmap); recognition(path); } catch (Exception e) { // TODO Auto-generatedcatch block e.printStackTrace(); } } else if (resultCode == RESULT_CANCELED) { log.d("用户取消了相册选择"); } break; } } /** * 拍照识别按钮响应 * * @param view */ public void takePhoto(View view) { if (TextUtils.isEmpty(FaceRecognitionManager.getInstance().getAccessToken())) { showToat("尚未获取到AccessToken"); return; } Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //系统常量, 启动相机的关键 openCameraIntent.putExtra("camerasensortype", 1); // 调用前置摄像头 ,红米Note 5// openCameraIntent.putExtra("autofocus", true); // 自动对焦// openCameraIntent.putExtra("fullScreen", false); // 全屏// openCameraIntent.putExtra("showActionIcons", false); startActivityForResult(openCameraIntent, TAKE_PHOTO_CODE); // 参数常量为自定义的request code, 在取返回结果时有用 } /** * 测试,从本地直接读取图片 * * @param view */ public void takefile(View view) { if (TextUtils.isEmpty(FaceRecognitionManager.getInstance().getAccessToken())) { showToat("尚未获取到AccessToken"); return; }// String path = "/mnt/sdcard/ytest/yjz.jpg";// recognition(path); //在这里跳转到手机系统相册里面 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, TAKE_LOCALFILE_CODE); } public void test(View view) { if (TextUtils.isEmpty(FaceRecognitionManager.getInstance().getAccessToken())) { showToat("尚未获取到AccessToken"); return; } File f = new File("/mnt/sdcard/ytest/4ren.jpg"); log.d("文件是否存在:" f.exists()); recognition("/mnt/sdcard/ytest/4ren.jpg"); } /** * 识别 * * @param bitmap */ private void recognition(Bitmap bitmap) { tv_message.setText(""); String groupid = FaceRecognitionManager.getInstance().getGroupid(); log.d("尝试识别 groupid =" groupid ",getQualityControl =" getQualityControl() ",getLivesControl =" getLivesControl() ",bitmap.getByteCount =" bitmap.getByteCount() ",ck_multi.isChecked():" ck_multi.isChecked()); Observable<Result> observable = null; if (ck_multi.isChecked()) { log.d("多人识别"); try { observable = FaceRecognitionManager.getInstance().searchMultiUser(bitmap, groupid, getQualityControl(), getLivesControl()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } } else { log.d("单人识别"); observable = FaceRecognitionManager.getInstance().searchUserByBitmap(bitmap, groupid, getQualityControl(), getLivesControl()); } observable.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Result>() { @Override public void accept(Result result) throws Exception { if (result.isSuccess()) {// JSONObject obj = result.getResult();// String user = obj.getJSONArray("user_list").getString(0);// HashMap<String, Object> map = GsonUtils.fromJson(user, mapToken.getType());// tv_userid.setText(map.get("user_id").toString());// tv_userinfo.setText(map.get("user_info").toString());// tv_score.setText(map.get("score").toString());// tv_message.setText(result.getResponse()); } else { showToat("code:" result.getCode() ",msg:" result.getErrorMsg()); } tv_message.setText(result.getResponse()); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { throwable.printStackTrace(); showToat("异常错误"); } }); } private void recognition(String imgeFilePath) { tv_message.setText(""); final long start = System.currentTimeMillis(); String groupid = FaceRecognitionManager.getInstance().getGroupid(); log.d("尝试识别 groupid =" groupid ",imgeFilePath=" imgeFilePath ",getQualityControl =" getQualityControl() ",getLivesControl =" getLivesControl()); Observable<Result> observable = null; if (ck_multi.isChecked()) { log.d("多人识别"); try { observable = FaceRecognitionManager.getInstance().searchMultiUser(imgeFilePath, groupid, getQualityControl(), getLivesControl()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } } else { log.d("单人识别"); observable = FaceRecognitionManager.getInstance().searchUserByImageFile(imgeFilePath, groupid, getQualityControl(), getLivesControl()); } observable.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Result>() { @Override public void accept(Result result) throws Exception { if (result.isSuccess()) {// JSONObject obj = result.getResult();// String user = obj.getJSONArray("user_list").getString(0);// HashMap<String, Object> map = GsonUtils.fromJson(user, mapToken.getType());// tv_userid.setText(map.get("user_id").toString());// tv_userinfo.setText(map.get("user_info").toString());// tv_score.setText(map.get("score").toString()); } else { showToat("code:" result.getCode() ",msg:" result.getErrorMsg()); } tv_message.setText(result.getResponse()); log.d("耗时:" (System.currentTimeMillis() - start)); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { throwable.printStackTrace(); showToat("异常错误"); } }); } private void showToat(String msg) { Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); log.d(msg); } private String getLivesControl() { switch (livesRadioGroup.getCheckedRadioButtonId()) { case R.id.radio_lives_none: return "NONE"; case R.id.radio_lives_low: return "LOW"; case R.id.radio_lives_normal: return "NORMAL"; case R.id.radio_lives_high: return "HIGH"; default: return "NONE"; } } private String getQualityControl() { switch (qualityRadioGroup.getCheckedRadioButtonId()) { case R.id.radio_lives_none: return "NONE"; case R.id.radio_lives_low: return "LOW"; case R.id.radio_lives_normal: return "NORMAL"; case R.id.radio_lives_high: return "HIGH"; default: return "NONE"; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复